package dao

import (
	"errors"
	"github.com/benji/software/model"
	log "github.com/sirupsen/logrus"
)

func CreateUser(user *model.User)error{
	log.Infof("User: %+v",user)
	if err := Db.Table("user").Save(user).Error;err !=nil{
		log.Errorf("Fail to create user, err: %v",err)
		return err
	}
	log.Infof("id: %v",user.ID)
	return nil
}
func GetUserByName(name string)(*model.User,error){
	res := make([]*model.User,0)
	if err := Db.Table("user").Where("name = ?",name).Find(&res).Error;err!=nil{
		log.Errorf("Fail to Get User name: ",err)
		return nil,err
	}
	if  len(res) == 0 {
		return nil, errors.New("User not exist")
	}
	return res[0],nil
}
func GetUserByID(id int)(*model.User,error){
	user := model.User{}
	if err := Db.Table("user").Where("id = ?",id).Find(&user).Error;err != nil {
		log.Errorf("Fail to get user from user table,err :%v",err)
		return nil,err
	}
	return &user, nil
}